/********************************************************************************
 * @Author: Zhenzhou Liu
 * @Date: 2024-11-19
 * @Description: Cg动态规划之钢条切割
 *******************************************************************************/
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

void steerBarCut(vector<int>& price) {
    int n = price.size();
    vector<int> dp(n, 0);
    for (int i = 1; i < n; ++i) {
        dp[i] = price[i];
        for (int j = 1; j < i; ++j) {
            int k = price[j] + dp[i - j];
            if (k > dp[i]) {
                dp[i] = k;
            }
        }
    }
    cout << dp[n - 1];
}

int main() {
    string input;
    cin >> input;
    size_t semiColonPos = input.find(';');
    int n = stoi(input.substr(0, semiColonPos));
    string pricePart = input.substr(semiColonPos + 1);
    stringstream ss(pricePart);
    vector<int> prices(n + 1);
    string segment;
    while (getline(ss, segment, ';')) {
        if (!segment.empty()) {
            size_t commaPos = segment.find(',');
            int i = stoi(segment.substr(0, commaPos));
            int p = stoi(segment.substr(commaPos + 1));
            prices[i] = p;
        }
    }
    steerBarCut(prices);
    return 0;
}